package com.wx.disparent.mallparent.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.disparent.mallparent.commons.inface.IBaseService;
import com.wx.disparent.mallparent.commons.util.MD5Utils;
import com.wx.disparent.mallparent.dao.FrontUserMapper;
import com.wx.disparent.mallparent.entity.ProblemEntity;
import com.wx.disparent.mallparent.entity.UserEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class FrontUserService implements IBaseService<UserEntity> {

    @Autowired
    private FrontUserMapper frontUserMapper;

    public Map<String, String> register(UserEntity userEntity) {
        String phone = userEntity.getPhone();
        HashMap<String, String> aMap = new HashMap<>();
        aMap.put("phone", phone);
        UserEntity byMap = frontUserMapper.findByMap(aMap);
        HashMap<String, String> map = new HashMap<>();
        if (byMap == null) {
            userEntity.setPasswd(MD5Utils.stringToMD5(userEntity.getPasswd()));
            frontUserMapper.insert(userEntity);
            map.put("isSuccess", "true");
            map.put("message", null);
        } else {
            map.put("isSuccess", "false");
            map.put("message", "该号码已被注册");
        }
        return map;
    }

    @Override
    public List<UserEntity> findAll(Map<String, Object> params) {
        return null;
    }

    @Override
    public UserEntity findById(int id) {
        UserEntity byId = frontUserMapper.findById(id);
        return byId;
    }

    public UserEntity SumWelfareMoney(int id) {
        Map<String,Object> map = new HashMap<>();
        map.put("referId",id);
        UserEntity userEntity = frontUserMapper.SumWelfareMoney(map);
        return userEntity;
    }

    /**
     * 安全问题验证
     *
     * @param userEntity 用户信息（phone,problem,answer）
     * @return map
     */
    public Map verificationByProblem(UserEntity userEntity) {
        Map<String, Object> map = new HashMap();
        UserEntity userEntityOld = frontUserMapper.findByPhone(userEntity.getPhone());
        if (userEntityOld != null) {
            if (userEntity.getProblem().equals(userEntityOld.getProblem()) && userEntity.getAnswer().equals(userEntityOld.getAnswer())) {
                map.put("isSuccess", "true");
                map.put("message", "验证通过");
                map.put("id", userEntityOld.getId());
            } else {
                map.put("isSuccess", "false");
                map.put("message", "答案错误");
                map.put("seat","问题位置");
            }
        } else {
            map.put("isSuccess", "false");
            map.put("message", "该用户不存在");
            map.put("seat","用户位置");
        }
        return map;
    }

    @Override
    public int insert(UserEntity userEntity) {
        return 0;
    }

    @Override
    public int update(UserEntity userEntity) {

        return 0;
    }

    @Override
    public int deleteById(int t) {
        return 0;
    }

    public UserEntity findByUsername(String username) {
        return null;
    }

    public Map<String, Object> login(UserEntity userEntity) {

        String phone = userEntity.getPhone();
        String passwd = MD5Utils.stringToMD5(userEntity.getPasswd());
        HashMap<String, String> loginMap = new HashMap<>();
        loginMap.put("phone", phone);
        loginMap.put("passwd", passwd);
        UserEntity userEntity1 = frontUserMapper.findByLoginMap(loginMap);

        HashMap<String, Object> map = new HashMap<>();
        if (userEntity1 == null) {
            map.put("isSuccess", "false");
            map.put("message", "手机号码或密码错误");
            map.put("userEntity", null);
        } else {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String lastLogin = sf.format(new Date());
            Integer id = userEntity1.getId();
            HashMap<String, Object> lastMap = new HashMap<>();
            lastMap.put("id",id);
            lastMap.put("lastLogin",lastLogin);
            Integer updateLastLogin = frontUserMapper.updateLastLogin(lastMap);
            UserEntity byId = frontUserMapper.findById(id);
            map.put("isSuccess", "true");
            map.put("message", null);
            map.put("userEntity", byId);
        }
        return map;
    }

    public PageInfo getLowerUsers(int id, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);

        List<UserEntity> list = frontUserMapper.getLowerUsers(id);

        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    public Map<String, Object> userInfoCheck(UserEntity userEntity) {
        Map<String, Object> map = new HashMap<>();
        UserEntity userNameEntity = null;
        if (StringUtils.isNotBlank(userEntity.getUsername())) {
            userNameEntity = frontUserMapper.findByUserName(userEntity.getUsername());
        }

        UserEntity byId = frontUserMapper.findById(userEntity.getId());
        if (userNameEntity == null || userEntity.getUsername().equals(byId.getUsername())) {
            map.put("userNameCheck", "true");
            map.put("userNameMessage", null);
        } else {
            map.put("userNameCheck", "false");
            map.put("userNameMessage", "用户名重复");
        }

        UserEntity emailEntity = frontUserMapper.findByEmail(userEntity.getEmail());
        if (emailEntity == null || userEntity.getEmail().equals(byId.getEmail())) {
            map.put("emailCheck", "true");
            map.put("emailMessage", null);
        } else {
            map.put("emailCheck", "false");
            map.put("emailMessage", "电子邮箱已被注册");
        }
        return map;
    }

    public Integer updateById(UserEntity userEntity) {
        Integer i = frontUserMapper.updateById(userEntity);
        return i;
    }


    public Integer updateByIdPayAcct(UserEntity userEntity) {
        Integer i = frontUserMapper.updateByIdPayAcct(userEntity);
        return i;
    }


    public Integer updatePassWdById(UserEntity userEntity) {
        userEntity.setPasswd(MD5Utils.stringToMD5(userEntity.getPasswd()));
        return frontUserMapper.updatePassWdById(userEntity);
    }

    public Map<String, Object> updatePwCheck(UserEntity userEntity) {
        UserEntity byId = frontUserMapper.findById(userEntity.getId());
        Map<String, Object> map = new HashMap<>();

        if(byId.getPasswd().equals(MD5Utils.stringToMD5(userEntity.getPasswd()))){
            map.put("pwCheck","true");
            map.put("pwMessage",null);
        }else{
            map.put("pwCheck","false");
            map.put("pwMessage","旧密码输入错误，请确认后输入");
        }
        return map;
    }

    public String findByProblem(String problem) {
        String problemInfo = frontUserMapper.findByProblem(problem);
        return problemInfo;
    }

    public List<ProblemEntity> findAllProblem() {

        List<ProblemEntity> problemEntityList = frontUserMapper.findAllProblem();
        return problemEntityList;

    }

    public List<UserEntity> findLowerUsers(Integer id) {
        List<UserEntity> lowerList = frontUserMapper.findLowerUsers(id);
        return lowerList;
    }
}
